package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.controller.EntityManagerFactoryUtil;
import csc.daonjpa.java.domain.Customer;

@Component
public class CustomerDAO {
	public Customer getCustomers(String loginId) {
		// Obtains entity manager object
				EntityManager entityManager = EntityManagerFactoryUtil
						.createEntityManager();

				// Obtains transaction from entity manager
				EntityTransaction entr = entityManager.getTransaction();

				// -----------Begin transaction-----------
				Customer customer = null;
				try {
					entr.begin();
					// Get a list of accounts from DB

					TypedQuery<Customer> query = entityManager.createQuery("SELECT c FROM " + Customer.class.getName() + " c Where c.loginId= :loginId", Customer.class);
					query.setParameter("loginId", loginId);

					customer = (Customer) query.getSingleResult();
					entr.commit();
				} catch (Exception e) {
					e.printStackTrace();
					entityManager.close();
				}
				return customer;
			
		}
	
	public Customer checkLogin(String loginId, String password) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		
		Customer customer = null;
		try {
			entr.begin();
			// Get a list of accounts from DB

			TypedQuery<Customer> query = entityManager.createQuery(
					"SELECT c FROM " + Customer.class.getName()
							+ " c Where c.loginId= :loginId and c.password= :password", Customer.class);
			query.setParameter("loginId", loginId);
			query.setParameter("password", password);
			customer = query.getSingleResult();
			entr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			entityManager.close();
		}
		// -----------End transaction-----------

		return customer;

	}
	
	public boolean modifyCustomer(Customer customer) {

		// Obtains entity manager object
		EntityManager entityManager = EntityManagerFactoryUtil
				.createEntityManager();

		// Obtains transaction from entity manager
		EntityTransaction entr = entityManager.getTransaction();

		// -----------Begin transaction-----------
		boolean res =  true;
		try {
			entr.begin();
			String temp = "UPDATE " + Customer.class.getName() + 
					" SET password = :password, "
					+ " firstName = :firstName , lastName = :lastName, "
					+ " address= :address , phone = :phone "
					+ "WHERE loginId = :loginId";
			Query query = entityManager.createQuery(temp);
			
			query.setParameter("loginId", customer.getLoginId());
			query.setParameter("password", customer.getPassword());
			query.setParameter("firstName", customer.getFirstname());
			query.setParameter("lastName", customer.getLastname());
			query.setParameter("address", customer.getAddress());
			query.setParameter("phone", customer.getPhone());

			query.executeUpdate();
			entr.commit();
		} catch (Exception e) {
			entityManager.close();
			res = false;
		}
		// -----------End transaction-----------
		return res;
	} 
}
